package com.aerfafish.spider.neo4j.spider.service.impl;

import com.aerfafish.spider.neo4j.dao.CompanyManageFund;
import com.aerfafish.spider.neo4j.repository.CompanyManageFundRepository;
import com.aerfafish.spider.neo4j.spider.service.CompanyFundRelationService;
import org.neo4j.driver.exceptions.TransientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.stereotype.Service;

@Service
public class CompanyFundRelationServiceImpl implements CompanyFundRelationService {

    @Autowired
    CompanyManageFundRepository companyManageFundRepository;

    @Override
    public void save(CompanyManageFund companyManageFund) throws Exception {
        save(companyManageFund.getCompany().getCode(), companyManageFund.getFund().getCode());
    }

    @Override
    public void save(String companyCode, String fundCode) throws Exception {
        if (companyCode == null || fundCode == null) {
            throw new Exception("some param is null");
        }
        try {
            companyManageFundRepository.save(companyCode, fundCode);
        } catch (TransientException | InvalidDataAccessApiUsageException e) {
            try {
                companyManageFundRepository.save(companyCode, fundCode);
            } catch (TransientException | InvalidDataAccessApiUsageException e0) {
                companyManageFundRepository.save(companyCode, fundCode);
            }
        }

    }
}
